package system

import (
	"gin-admin/common/dto/request"
	"gin-admin/common/dto/response"
	"gin-admin/core"
	serviceRequest "gin-admin/service/dto/request"
	serviceResponse "gin-admin/service/dto/response"
	"gin-admin/service/v2/system"

	"github.com/gin-gonic/gin"
)

type UserHandler struct {
	UserService system.UserService
}

func NewUserHandlerApp(service *system.UserService) *UserHandler {
	return &UserHandler{UserService: *service}
}

// @Summary 创建用户信息(v2)
// @Tags /v2/用户管理
// @Accept json
// @Product json
// @Param data body request.CreateUserRequest true "创建用户详情请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/user/create [post]
func (uc *UserHandler) CreateUserHandler(ctx *gin.Context) {
	var createUserRequest serviceRequest.CreateUserRequest
	if !core.ValidateParams(ctx, &createUserRequest) {
		return
	}
	if err := uc.UserService.CreateUserService(&createUserRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 删除用户信息(v2)
// @Tags /v2/用户管理
// @Accept json
// @Product json
// @Param data body request.IDRequest true "删除用户详情请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/user/delete [delete]
func (uc *UserHandler) DeleteUserByIdHandler(ctx *gin.Context) {
	var iDRequest request.IDRequest
	if !core.ValidateParams(ctx, &iDRequest) {
		return
	}
	responseData, err := uc.UserService.DeleteUserByIdService(&iDRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 更新用户信息(v2)
// @Tags /v2/用户管理
// @Accept json
// @Product json
// @Param data body request.ModifyUserRequest true "更新用户详情请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/user/modify [put]
func (uc *UserHandler) ModifyUserHandler(ctx *gin.Context) {
	var modifyRequest serviceRequest.ModifyUserRequest
	if !core.ValidateParams(ctx, &modifyRequest) {
		return
	}
	if err := uc.UserService.ModifyUserService(&modifyRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 获取用户详情(v2)
// @Tags /v2/用户管理
// @Accept json
// @Produce  json
// @Param id path string true "获取用户详情请求参数"
// @Success 200 {object} response.Response{data=response.UserResponse} "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/user/info/{id} [get]
func (uc *UserHandler) GetUserInfoHandler(ctx *gin.Context) {
	id := ctx.Param("id")
	if !core.ValidateParams(ctx, id) {
		return
	}
	userResponse, err := uc.UserService.GetUserInfoService(id)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, userResponse)
}

// @Summary 获取用户分页列表(v2)
// @Tags /v2/用户管理
// @Accept json
// @Produce  json
// @Param page query int true "页码"
// @Param size query int true "每页大小"
// @Param status query int true "状态"
// @Param account query string true "账号"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/user/page [get]
func (uc *UserHandler) GetUserPageHandler(ctx *gin.Context) {
	var queryRequest serviceRequest.UserQueryRequest
	if !core.ValidateParams(ctx, &queryRequest) {
		return
	}
	result, err := uc.UserService.GetUserPageService(&queryRequest)
	if err != nil {
		response.SuccessNoData(ctx)
		return
	}
	response.SuccessWithPage[serviceResponse.UserResponse](ctx, *result)
}

// @Summary 获取用户列表(v2)
// @Tags /v2/用户管理
// @Accept json
// @Produce  json
// @Param account query string true "账号"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/user/list [get]
func (uc *UserHandler) GetListPageHandler(ctx *gin.Context) {
	var queryRequest serviceRequest.UserQueryRequest
	if !core.ValidateParams(ctx, &queryRequest) {
		return
	}
	responseData, err := uc.UserService.GetUserListService(&queryRequest)
	if err != nil {
		response.SuccessNoData(ctx)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 用户登录(v2)
// @Tags /v2/用户管理
// @Accept json
// @Produce  json
// @Param data body request.LoginRequest true "登录请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/user/login [post]
func (uc *UserHandler) UserLoginHandler(ctx *gin.Context) {
	var loginRequest serviceRequest.LoginRequest
	if !core.ValidateParams(ctx, &loginRequest) {
		return
	}
	responseData, err := uc.UserService.UserLoginService(&loginRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}
